/******************************************************
/	Cody Brenneman		12/20/2006
/	Skip Away prototype control in C
/
*******************************************************/

/*
#pragma config WDT = OFF
#pragma config OSC = INTIO2, LVP = OFF
#pragma config MCLRE = OFF
#pragma config FOSCSEL = 0x000001
 */

#include <p30f4013.h>
#include <adc12.h>
#include "Initializations.h"
#include "Delays.h"
#include "Interrupt Service Routines.h"
#include "LCD_Controls.h"
#include "Variables.h"



/* Setting the configuration registers  */
//_FBORPOR( PWRT_OFF & BORV45 & PBOR_OFF & MCLR_DIS )
_FBORPOR( PWRT_OFF & BORV45 & PBOR_OFF & MCLR_EN )
_FWDT(WDTPSB_1 & WDTPSA_1 & WDT_OFF)
_FOSC(FRC_PLL16)





/* Constants */
#define ON  1
#define OFF 0


#define SWITCHING_PIN       LATDbits.LATD2
#define BUTTON              PORTFbits.RF0



/* Function prototypes */
int main(void);
void InterruptHandlerHigh(void);
//void ConvertToNumber(unsigned char*);
void DisplayTemperature(float temperature);



/* High priority interrupt vector */
//#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
    /*
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
     */
}


//--------------------------------------------------------------------------

/* Global variables */
unsigned char quarter_second = 0;
unsigned char one_minute_counter = 0;
unsigned char one_minute_flag = 0;
unsigned char minutes = 0;
unsigned char timer_flag = 0;
unsigned char current_addr = 0;



/* The main function should only be a base for
	calling other functions.  It will stay in an
	infinite loop */

//#pragma code main = 0x2A
/* make sure to start the main program in the proper
	area of memory! Code memory starts at 0x2A */


int main(void)
{

    int ADC_data = 0;
    int temp = 0;
    unsigned char data_char[12];
    int i = 0;
    float temperature = 0;

	oscillator_init();
	port_init();
	interrupt_init();
	timer2_init();
        AD_init();

        tenth_seconds(5);

        LCD_init();
	//timer1_init();


        tenth_seconds(1);



        LCDaddress(LINE1);
        LCDwrite("Push the button");
        LCDaddress(LINE2);
        LCDwrite("below...");
        
        while(BUTTON);
        milliseconds(500);
        while(!BUTTON);
        LCDclear();


        LCDaddress(LINE1);
        LCDwrite("Check it out");
        LCDaddress(LINE2);
        LCDwrite("Connor...");


        while(BUTTON);
        milliseconds(500);
        while(!BUTTON);
        LCDclear();


        LCDaddress(LINE1);
        LCDwrite("It measures the");
        LCDaddress(LINE2);
        LCDwrite("temperature");


        while(BUTTON);
        milliseconds(500);
        while(!BUTTON);
        LCDclear();


/*
        while(1)
        {
            if(ADCON1bits.DONE)
            {
                ADC_data = ADCBUF0;

                temp = ADC_data & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[2] = temp + 48;
                }
                else
                {
                    data_char[2] = temp + 55;
                }

                temp = (ADC_data>>4) & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[1] = temp + 48;
                }
                else
                {
                    data_char[1] = temp + 55;
                }

                temp = (ADC_data>>8) & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[0] = temp + 48;
                }
                else
                {
                    data_char[0] = temp + 55;
                }

                LCDclear();
                LCDwrite("    ");

                for(i=0;i<3;i++)
                {
                    LCDchar(data_char[i],1);
                }

                milliseconds(500);
            }

        }

    */

        while(1)
        {
            
            if(ADCON1bits.DONE)
            {
                ADC_data = ADCBUF0;

                LCDclear();
                
                temp = ADC_data & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[2] = temp + 48;
                }
                else
                {
                    data_char[2] = temp + 55;
                }

                temp = (ADC_data>>4) & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[1] = temp + 48;
                }
                else
                {
                    data_char[1] = temp + 55;
                }

                temp = (ADC_data>>8) & 0b1111;
                if(temp<10)
                {   //do 2 first, because it writes backwards on the LCD
                    data_char[0] = temp + 48;
                }
                else
                {
                    data_char[0] = temp + 55;
                }

                LCDclear();
                LCDwrite("    ");

                for(i=0;i<3;i++)
                {
                    LCDchar(data_char[i],1);
                }

                //while(BUTTON);

                     
                


                for(i=0; i<1129; i++)
                {
                    if(ADCBUF0 > ADC_codes[i])
                    {
                        break;
                    }
                }
                //take the F temperature right now
                if(i>0)
                {
                    i = i - 1;
                }
                temperature = Temperatures[i][1];

                //temperature = 13.7;

                //temperature = ADC_data;
                DisplayTemperature(temperature);

                milliseconds(500);

            }
      

        }

        return(0);
}

	
void DisplayTemperature(float temperature)
{
    int integer = 0;
    int decimal = 0;
    unsigned char i = 0;
    unsigned char data_char[6];
    
    integer = temperature;
    temperature = temperature - integer;
    temperature = temperature * 10;
    decimal = temperature;
    
    //so after all that, integer should be the integer temperature,
    //  and decimal should be everything after the decimal point, yes?
    // ex: temperature = 112.37
    //   -> integer = 112
    //   -> decimal = 37


    data_char[0] = (decimal % 10) + 48;
    //decimal = decimal / 10;
    //data_char[1] = (decimal % 10) + 48;

    data_char[1] = '.';

    data_char[2] = (integer % 10) + 48;
    integer = integer / 10;
    data_char[3] = (integer % 10) + 48;
    integer = integer / 10;
    data_char[4] = (integer % 10) + 48;

    LCDaddress(LINE2);

    for(i=0; i<5; i++)
    {
        LCDchar(data_char[4-i],1);
    }


    LCDaddress(LINE1);

    return;
}
	



//--------------------------------------------------------------------------
//		Interrupt handling
//--------------------------------------------------------------------------


/* High priority interrupt routine */

//#pragma code
//#pragma interrupt InterruptHandlerHigh

/* Handles all high priority interrupts
	so check which interrupt has occurred
	and then service it */
void InterruptHandlerHigh(void)
{
/*
    if (INTCONbits.TMR0IF)
    {                       
		INTCONbits.TMR0IF = 0;		//reset it           
		Timer0ISR();
	}

	if(PIR1bits.TMR1IF)
	{
		PIR1bits.TMR1IF = 0;
		Timer1ISR();		
	}


	if(INTCONbits.INT0IF)
	{
		LED = 1;					//turn on LED
		while(!SLEEP_BUTTON);
		LED = 0;					//until sleep button is released
		INTCONbits.INT0IF = 0;		//clear the interrupt flag
		INTCON3bits.INT1IF = 0;
		T0CONbits.TMR0ON = 1;		//Start timer 0
		timer_flag = 1;				//So i know it's timing something now
	}
*/
}


